package com.zhengsanming.service;

import com.zhengsanming.mapper.UserMapper;
import com.zhengsanming.pojo.Brand;
import com.zhengsanming.pojo.User;
import com.zhengsanming.util.SqlsessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServive {
    SqlSessionFactory factory = SqlsessionFactoryUtils.getSqlSessionFactory();

    /*
     *
     * 登录方法
     * */
    public boolean login(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper.selectAll(user);
        if (user1 == null) {
            return false;
        } else {
            return true;
        }

    }

    /*
     *
     * 注册
     * */
    public Boolean register(User user) {
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user1 = mapper.selectByUsername(user.getUsername());
        if (user1 == null) {
            //用户不存在
            mapper.insertUser(user);
            sqlSession.commit();

        }
        sqlSession.close();
        return user1 == null;
    }
}
